package com.crm.controller.common;

import com.crm.DTO.LoginDTO;
import com.crm.annotation.BusinessType;
import com.crm.annotation.Log;
import com.crm.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * \* User: chy
 * \* Date: 2025/9/4  10:02
 * \* Description:
 * \
 */
@RestController
@RequestMapping
@Slf4j
public class LoginController {
    @Autowired
    private UserService userService;

    /**
     * 登录
     *
     * @param loginDTO
     * @return
     */
    @PostMapping("/login")
    public HashMap<String, Object> login(@RequestBody LoginDTO loginDTO) {
        log.info("用户:{}进行登录", loginDTO);
        HashMap<String, Object> token = userService.login(loginDTO);
        return token;
    }

    /**
     * 获取用户权限信息
     *
     * @return
     */
    @GetMapping("/getInfo")
    public HashMap<String, Object> getInfo() {
        HashMap<String, Object> map = new HashMap<>();

        // 创建用户信息Map
        Map<String, Object> user = new HashMap<>();
        user.put("createBy", "admin");
        user.put("createTime", "2021-03-31 03:12:10");
        user.put("updateBy", null);
        user.put("updateTime", null);
        user.put("userId", 1);
        user.put("userName", "admin");
        user.put("nickName", "admin");
        user.put("email", "admin@163.com");
        user.put("phonenumber", "15888888888");
        user.put("sex", "1");
        user.put("status", "0");
        user.put("delFlag", "0");
        user.put("loginIp", "127.0.0.1");
        user.put("loginDate", "2021-03-31T03:12:10.000+0800");
        user.put("salt", null);
        user.put("roleIds", null);

        // 设置部门信息
        Map<String, Object> dept = new HashMap<>();
        dept.put("createBy", null);
        dept.put("createTime", null);
        dept.put("updateBy", null);
        dept.put("updateTime", null);
        dept.put("deptId", 103);
        dept.put("parentId", 101);

        user.put("dept", dept);

        // 设置角色信息
        String[] roles = {"admin"};
        user.put("roles", roles);

        // 设置权限信息
        String[] permissions = {"*:*:*"};

        // 构造返回结果
        map.put("code", 200);
        map.put("msg", "操作成功");
        map.put("user", user);
        map.put("roles", roles);
        map.put("permissions", permissions);
        map.put("admin", true);
        map.put("avatar", "");

        return map;
    }

    /**
     * 登出
     *
     * @return
     */
    @PostMapping("/logout")
    public HashMap<String, Object> logout() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "操作成功");
        return map;
    }

}
